﻿Public Class frmElevesDebit
    Public pNo_ligne As Integer

    Private szSQL As String
    Private rs As ADODB.Recordset

    Private Sub frmElevesDebit_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        TE_DATE.Focus()
    End Sub

    Private Sub frmElevesDebit_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        CompteToScreen()

        If gUserAcces < 9 Then
            ' interdire la saisie de
            TE_DATE.Enabled = False
            TE_DATE_VAL.Enabled = False
            TE_DEBIT.Enabled = False
            TE_MT_VAL.Enabled = False
            TE_COMPTE.Enabled = False
            GB_HEURE.Enabled = False
        Else
            TE_DATE.Enabled = True
            TE_DATE_VAL.Enabled = True
            TE_DEBIT.Enabled = True
            TE_MT_VAL.Enabled = True
            TE_COMPTE.Enabled = True
            GB_HEURE.Enabled = True
        End If

        BO_ENREGISTRER.Enabled = True
        BO_SUPPRIMER.Enabled = True
        ' journée déjà comptabilisée ?
        If TE_DATE_VAL.Text.Length > 0 Then
            If IsExported(CDate(TE_DATE_VAL.Text)) Then
                BO_ENREGISTRER.Enabled = False
                BO_SUPPRIMER.Enabled = False
            End If
        End If
    End Sub

    Private Sub CompteToScreen()

        ' initialiser les tarifs selon la filière de l'élève
        TE_CD_CLI.Text = gCD_CLI
        InitComboTarifs(CB_TARIFS, TE_CD_CLI.Text)

        If pNo_ligne = 0 Then
            ' creation
            TE_DATE.Text = Now.ToShortDateString
            CB_TARIFS.SelectedIndex = CB_TARIFS.FindString("LCB")
            TE_NOPLAN.Clear()
        Else
            ' Lire l'enreg sélectionné
            szSQL = "CALL spGet_ELEVES_CPT_byNO_LIGNE(" & pNo_ligne & ")"
            GetRecordset(ctDSN_AEM, szSQL, rs)
            If rs.RecordCount > 0 Then
                TE_CD_CLI.Text = rs.Fields("cd_cli").Value
                TE_DATE.Text = rs.Fields("date").Value
                CB_TARIFS.SelectedIndex = CB_TARIFS.FindString(nv(rs.Fields("ref").Value))
                TE_LIBELLE.Text = nv(rs.Fields("intitule").Value)
                TE_QTE.Text = rs.Fields("qte").Value
                TE_DEBIT.Value = rs.Fields("debit").Value
                TE_TVA.Text = "TVA" & rs.Fields("ctva").Value
                TE_DATE_VAL.Text = IfDate(rs.Fields("date_valeur").Value)
                TE_MT_VAL.Value = rs.Fields("mtval").Value
                TE_COMPTE.Text = nz(rs.Fields("compte").Value)
                TE_NOPLAN.Text = rs.Fields("noplan").Value
                TE_STATUT.Text = rs.Fields("statut").Value
                ' ChkCIRCUIT.Checked = rs.Fields("circuit").Value
                TE_MODIFIE_LE.Text = setModifiedBy(rs.Fields("cree_le").Value, rs.Fields("modif_le").Value, rs.Fields("cd_uti").Value)
            End If
            rs.Close()
            TE_NOLIG.Text = pNo_ligne
        End If

        ' si heure de planning, interdire
        If TE_NOPLAN.TextLength <> "0" Then
            If gUserAcces < 9 Then
                CB_TARIFS.Enabled = False
                TE_LIBELLE.Enabled = False
            Else
                CB_TARIFS.Enabled = True
                TE_LIBELLE.Enabled = True
            End If
            TE_QTE.Enabled = False
        Else
            TE_LIBELLE.Enabled = True
            CB_TARIFS.Enabled = True
            TE_QTE.Enabled = True
        End If

    End Sub

    Private Sub CB_TARIFS_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CB_TARIFS.SelectedIndexChanged
        Dim rsTarif As ADODB.Recordset = Nothing

        ' Lire le TARIF
        szSQL = "CALL spGet_TARIFS('" & getCodeFromCombo(CB_TARIFS.SelectedItem) & "')"
        GetRecordset(ctDSN_AEM, szSQL, rsTarif)
        If rsTarif.RecordCount > 0 Then
            TE_LIBELLE.Text = rsTarif.Fields("lib").Value
            TE_DEBIT.Value = rsTarif.Fields("puttc").Value
            TE_QTE.Value = 1
            TE_TVA.Text = "TVA" & rsTarif.Fields("ctva").Value
            TE_COMPTE.Text = rsTarif.Fields("compte").Value
            ' si heure de conduite, statut = prise
            If rsTarif.Fields("fam").Value = "H" Then
                TE_STATUT.Text = "1"
            Else
                TE_STATUT.Text = "0"
            End If
            Select Case rsTarif.Fields("fam").Value
                Case "F"
                    TE_MT_VAL.Value = 0
                    TE_DATE_VAL.Text = TE_DATE.Text

                Case "S"
                    TE_MT_VAL.Value = 0
                    TE_DATE_VAL.Text = ""

                Case Else
                    TE_MT_VAL.Value = TE_DEBIT.Value
                    TE_DATE_VAL.Text = TE_DATE.Text

            End Select
        End If
        rsTarif.Close()

    End Sub

    Private Sub TE_QTE_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TE_QTE.LostFocus

        ' seul les administrateurs peuvent saisir des qté négatives
        If gUserAcces < 9 And TE_QTE.Value < 0 Then
            MsgBox("Vous n'êtes pas autorisé à saisir une quantité négative !", MsgBoxStyle.Critical)
            TE_QTE.Focus()
        End If

    End Sub

    Private Sub BO_ENREGISTRER_Click(sender As System.Object, e As System.EventArgs) Handles BO_ENREGISTRER.Click

        Dim szDateValeur As String

        If IsDate(TE_DATE_VAL.Text) Then
            szDateValeur = "'" & Format(CDate(TE_DATE_VAL.Text), "yyyy/MM/dd") & "'"
        Else
            szDateValeur = "NULL"
        End If
        ' puis mise à jour 
        szSQL = "CALL spUpd_ELEVES_DEBIT(" & pNo_ligne & "," & gCD_CLI & ",'" & Format(TE_DATE.Value, "yyyy/MM/dd") & "','" & getCodeFromCombo(CB_TARIFS.SelectedItem) & "','" & _
            TE_LIBELLE.Text & "'," & TE_QTE.Value & "," & TE_DEBIT.Value & "," & szDateValeur & "," & _
            TE_MT_VAL.Value & "," & CInt(TE_COMPTE.Text) & "," & TE_STATUT.Text & ",0," & gUserAgence & ",'" & gUserCode & "')"
        If ExecQuery(ctDSN_AEM, szSQL) Then
            Me.Close()
        End If

    End Sub

    Private Sub BO_SUPPRIMER_Click(sender As System.Object, e As System.EventArgs) Handles BO_SUPPRIMER.Click
        ' utilisateur autorisé à supprimer un ligne ?
        If IsUserAuthorised(9) = False Then
            Exit Sub
        End If
        ' suppression
        deleteEnregistrement("eleves_cpt", TE_LIBELLE.Text, pNo_ligne)
        ' réaffiche la viewlist
        Me.Close()
    End Sub
End Class